package cn.dankal.flyaudio.presenter;

import android.content.Intent;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.jyj.recruitment.wxapi.PayUtils;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import api.ShopUserServiceFactory;
import cn.dankal.basiclib.base.recyclerview.BaseRecyclerViewPresenter;
import cn.dankal.basiclib.pojo.BaseListResponse;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.pojo.CreateOrderPreviewEntity;
import cn.dankal.basiclib.pojo.WexinPayResult;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.SharePreferenceManager;
import cn.dankal.business.pojo.OrderBean;
import cn.dankal.flyaudio.FlyApplication;
import cn.dankal.flyaudio.contract.MyCardContract;
import cn.dankal.flyaudio.contract.MyOrderContract;
import cn.dankal.flyaudio.friend.pickimage.StringUtil;
import cn.dankal.flyaudio.personal.driverlisense.LisensePresenter;
import cn.dankal.flyaudio.store.PayResultActivity;

public class MyOrderPresenter implements MyOrderContract.Presenter {
    private MyOrderContract.View mView;

    private int page = 1;
    private int size = 10;

    private String currentState = "";


    @Override
    public void attachView(@NonNull MyOrderContract.View view) {
        this.mView = view;
    }

    @Override
    public void detachView() {
        this.mView = null;
    }

    @Override
    public void getMyOrder(String state, boolean isRefresh) {
        if(!state.equals("-1")){
            this.currentState = state;
        }

        if (isRefresh) {
            page = 1;
        }
        mView.showLoadingDialog();
        ShopUserServiceFactory.getOrders(page, size, currentState)
                .subscribe(new AbstractSubscriber<BaseResponse<BaseListResponse<CreateOrderPreviewEntity>>>(mView) {
                    @Override
                    public void onNext(BaseResponse<BaseListResponse<CreateOrderPreviewEntity>> baseListResponseBaseResponse) {
                        mView.dismissLoadingDialog();
                        mView.showOrderList(baseListResponseBaseResponse.getData().getList(), isRefresh);
                        page++;
                    }
                });
    }

    @Override
    public void wechatPay(String uuid) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.wx(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<WexinPayResult>>(mView) {
                    @Override
                    public void onNext(BaseResponse<WexinPayResult> objectBaseResponse) {
                        mView.dismissLoadingDialog();
                        Log.d("ConfirmOrderPresenter", JSON.toJSONString(objectBaseResponse));
                        IWXAPI iwxapi = WXAPIFactory.createWXAPI(FlyApplication.getContext(), objectBaseResponse.getData().getAppid(), true);
                        iwxapi.registerApp(objectBaseResponse.getData().getAppid());
                        SharePreferenceManager.setCachedAppKey(objectBaseResponse.getData().getAppid());
                        PayReq payReq = PayUtils.wechatPay(JSON.toJSONString(objectBaseResponse.getData()));
                        Log.d("ConfirmOrderPresenter", JSON.toJSONString(payReq));
                        iwxapi.sendReq(payReq);
                    }
                });
    }

    @Override
    public void alipay(String uuid) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.alipay(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<String>>(mView) {
                    @Override
                    public void onNext(BaseResponse<String> createOrderPreviewEntityBaseResponse) {
                        mView.dismissLoadingDialog();
                        Log.d("ConfirmOrderPresenter",JSON.toJSONString(createOrderPreviewEntityBaseResponse));
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                boolean isSuccess = PayUtils.aliPay(ActivityUtils.getTopActivity(),createOrderPreviewEntityBaseResponse.getData());
                                if(isSuccess){
                                    Intent jump = new Intent(ActivityUtils.getTopActivity(), PayResultActivity.class);
                                    jump.putExtra("isPaySuccess", true);
                                    jump.putExtra("orderUuid", uuid);
                                    ActivityUtils.startActivity(jump);
                                    mView.finishActivity();
                                    Log.d("ConfirmOrderPresenter","支付成功");
                                }else {
                                    Intent jump = new Intent(ActivityUtils.getTopActivity(), PayResultActivity.class);
                                    jump.putExtra("isPaySuccess", false);
                                    jump.putExtra("orderUuid", uuid);
                                    ActivityUtils.startActivity(jump);
                                    mView.finishActivity();
                                    Log.d("ConfirmOrderPresenter","支付失败");
                                }
                            }
                        }).start();
                    }
                });
    }

    @Override
    public void confirmReceiveStore(String uuid) {
        mView.showLoadingDialog();
        ShopUserServiceFactory.confirmReceive(uuid)
                .subscribe(new AbstractSubscriber<BaseResponse<Object>>(mView) {
                    @Override
                    public void onNext(BaseResponse<Object> objectBaseResponse) {
                        mView.dismissLoadingDialog();
                        getMyOrder(currentState, true);
                    }
                });
    }
}
